package com.cinc.mdm.modules.codeRule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cinc.mdm.modules.codeRule.entity.MdmCodeRule;
import com.cinc.mdm.modules.codeRule.enums.CodeTypeEnum;
import com.cinc.mdm.modules.codeRule.mapper.MdmCodeRuleMapper;
import com.cinc.mdm.modules.codeRule.service.IMdmCodeRuleService;
import com.cinc.mdm.modules.sysParam.service.IMdmSysParamService;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.time.LocalDate;
import java.util.Date;

/**
 * @Description: 编码规则
 * @Author: jeecg-boot
 * @Date: 2021-10-12
 * @Version: V1.0
 */
@Service
public class MdmCodeRuleServiceImpl extends ServiceImpl<MdmCodeRuleMapper, MdmCodeRule> implements IMdmCodeRuleService {

    /**
     * 编码生成
     *
     * @param codeType
     * @return java.lang.String
     */
    @Override
    public synchronized String generateCodeByCodeType(String codeType) {
        String code = null;
        LocalDate now = LocalDate.now();
        LambdaQueryWrapper<MdmCodeRule> codeRuleQueryWrapper = new LambdaQueryWrapper<>();
        codeRuleQueryWrapper.eq(MdmCodeRule::getCodeType, codeType).eq(MdmCodeRule::getEnableFlag, "1");
        MdmCodeRule mdmCodeRule = baseMapper.selectOne(codeRuleQueryWrapper);
        if (mdmCodeRule != null) {
            String prefix = StringUtils.isBlank(mdmCodeRule.getPrefix()) ? "" : mdmCodeRule.getPrefix();
            Integer curSeqNo = mdmCodeRule.getSeqNo();
            String dateStr = "";
            String dateFmt = mdmCodeRule.getDateFmt();
            if ("1".equals(mdmCodeRule.getDateFlag()) && StringUtils.isNotBlank(dateFmt)) {
                dateStr = DateUtils.formatDate(new Date(), mdmCodeRule.getDateFmt());
                // 序列号重置
                int year = mdmCodeRule.getYear() == null ? 0 : mdmCodeRule.getYear();
                int month = mdmCodeRule.getMonth() == null ? 0 : mdmCodeRule.getMonth();
                int day = mdmCodeRule.getDay() == null ? 0 : mdmCodeRule.getDay();
                if((dateFmt.contains("y") && now.getYear() != year)
                    || (dateFmt.contains("M") && now.getMonthValue() != month)
                    || (dateFmt.contains("d") && now.getDayOfMonth() != day)){

                    curSeqNo = 0;
                }
            }
            String newSeqNo = "";
            if (curSeqNo != null && mdmCodeRule.getSeqLen() != null) {
                newSeqNo = String.format("%0" + mdmCodeRule.getSeqLen() + "d", curSeqNo + 1);
            }
            code = prefix + dateStr + newSeqNo;

            // 更新最新序列号
            MdmCodeRule mdmCodeRuleModify = new MdmCodeRule();
            mdmCodeRuleModify.setId(mdmCodeRule.getId());
            mdmCodeRuleModify.setSeqNo(curSeqNo + 1);
            mdmCodeRuleModify.setYear(now.getYear());
            mdmCodeRuleModify.setMonth(now.getMonthValue());
            mdmCodeRuleModify.setDay(now.getDayOfMonth());
            baseMapper.updateById(mdmCodeRuleModify);
        }

        return code;
    }

    /**
     * 出入库单编号生成
     *
     * @return java.lang.String
     */
    @Override
    public String generateStockInOutCode() {
        return generateCodeByCodeType("16");
    }

    /**
     * 库存初始化单据编号生成
     *
     * @return java.lang.String
     */
    @Override
    public String generateStockInitCode() {
        return generateCodeByCodeType("19");
    }

    @Override
    public String generateFiPaymentVoucherCode() {
        return generateCodeByCodeType("33");
    }

    @Override
    public String generateFiReceiveVoucherCode() {
        return generateCodeByCodeType("34");
    }

    @Override
    public String generateMmPurReqNo() {
        return generateCodeByCodeType(CodeTypeEnum.TYPE14.getCode());
    }

    @Override
    public String generateSdContractNo() {
        return generateCodeByCodeType(CodeTypeEnum.TYPE14.getCode());
    }

    @Override
    public String generatePpWorkOrderReportNo() {
        return generateCodeByCodeType(CodeTypeEnum.TYPE48.getCode());
    }
}
